class GroupsController < ApplicationController
  before_filter :only_teachers,
                :except => [:show, :edit, :update, :confirm_remove_user, :remove_user]
  before_filter :get_project
  before_filter :get_class
  before_filter :get_group
  before_filter :is_teacher_or_owner,
                :only => [:edit, :update, :confirm_remove_user, :remove_user]
  before_filter :is_teacher_or_member
  
  # Shows groups list
  #
  def index
    @groups = @teaching.groups.find(:all)
    
    respond_to do |wants|
      wants.html
      wants.xml   { render :xml => @groups.to_xml }
    end
  end
  
  # Shows a single group
  #
  def show
    respond_to do |wants|
      wants.html
      wants.xml { render :xml => @group.to_xml }
    end
  end
  
  # Shows form to edit a group. Only for GET requests.
  #
  def edit
    
  end
  
  # Updates group
  #
  def update
    if @group.update_attributes(params[:group])
      respond_to do |wants|
        wants.html  { redirect_to group_url(@project, @teaching, @group) }
        wants.js
        wants.xml   { head :ok }
      end
    else
      respond_to do |wants|
        wants.html do
          flash.now[:error] = 'Unable to save group'
          render :action => "edit"
        end
        wants.js    { render :nothing => true, :status => 500 }
        wants.xml   { render :xml => @group.errors.to_xml }
      end
    end
  end
  
  # Shows form to create a new group. Only for GET requests.
  #
  def new
    @group = Group.new
  end
  
  # Creates new group
  #
  def create
    @group = @teaching.groups.build(params[:group])
    
    if @group.save
      respond_to do |wants|
        wants.html  { redirect_to group_url(@project, @teaching, @group) }
        wants.xml   { head :created, :location => group_url(@project, @teaching, @group) }
      end
    else
      respond_to do |wants|
        wants.html do
          flash.now[:error] = 'Unable to create group'
          render :action => "new"
        end
        wants.js    { render :nothing => true, :status => 500 }
        wants.xml   { render :xml => @group.errors.to_xml }
      end
    end
  end
  
  # Deletes group
  #
  def destroy
    @group.destroy
    
    respond_to do |wants|
      wants.html do
        flash[:note] = 'Group deleted'
        redirect_to groups_url(@project, @teaching)
      end
      wants.js  { head :ok }
      wants.xml { head :ok }
    end
  end
  
  # Shows files uploaded by all groups belonging to the selected class
  #
  def uploads
    respond_to do |wants|
      wants.html do
        @groups = @teaching.groups.find(:all,
                                        :conditions => ["uploads.final = 1"],
                                        :include => [{:uploads => :deadline}],
                                        :order => "groups.name, deadlines.end, uploads.title")
      end
      wants.xml do
        @uploads = Upload.find( :all,
                                :conditions => ["groups.teaching_id = ? AND uploads.final = 1", @teaching.id],
                                :include => [:group])
        render :xml => @uploads.to_xml
      end
    end
  end
  
  # Shows form to delete user from group
  #
  def confirm_remove_user
    @user = @group.users.find(:first, :conditions => {:id => params[:user_id]})
    
    if @user.nil?
      render :text => "The user you're looking for doesn't exist", :layout => true
    end
  end
  
  # Removes user from group
  #
  def remove_user
    user = @group.users.find(:first, :conditions => {:id => params[:user_id]})
    
    unless user.nil?
      @group.users.delete(user)
    end
    
    unless user && @group.is_owner?(user)
      respond_to do |wants|
        wants.html do
          flash[:note] = "Student deleted from group"
          redirect_to group_path(@project, @teaching, @group)
        end
        wants.js  { head :ok }
        wants.xml { head :ok }
      end
    else # Can't delete owner of the group
      respond_to do |wants|
        wants.html do
          flash[:error] = "You can't delete the owner of the group"
          redirect_to group_path(@project, @teaching, @group)
        end
        wants.js  { render :nothing => true, :status => 403 }
        wants.xml { render :nothing => true, :status => 403 }
      end
    end
  end
private

  # If params[:id] is set, retrieves @group
  #
  def get_group
    unless params[:id].nil? || params[:id].blank?
      @group = @teaching.groups.find(params[:id])
      raise if @group.nil?
    end
    return true
  rescue
    respond_to do |wants|
      wants.html { render :text => "The group you're looking for doesn't exist!", :layout => true, :status => 404 }
      wants.js   { render :nothing => true, :status => 404 }
      wants.xml  { head :not_found }
    end
    return false
  end
end
